<?php
/**
 * File: storage.php
 * Author: Johnny Mom
 * Created: 12/2/12
 * Description: storage module for the Internship Listing project. 
 * The database "internship2career" has a table "internship" where all 
 * internship postings are stored.
 */

require_once("util.php"); 
require_once("control.php"); 

/**
 * Inserts internship posting record based on the given $sponsor, $city, $date, 
 * $title, $category, and $isPaid parameter values.
 * Exists application if any database error occurs.
 * @param string $sponsor
 * @param string $city
 * @param string $date
 * @param string $title
 * @param string $category
 * @param string $isPaid
 */
 function insertInternship($sponsor, $city, $state, $date, $title, $category, 
                           $isPaid)
{
$conn = connect( ); //open connection
  $query = "insert into internship (sponsorName, sponsorCity, sponsorState, postingDate, internshipTitle, internshipCategory, isPaid) values ('$sponsor', '$city', '$state', '$date', '$title', '$category', '$isPaid')";
  $resource = mysqli_query($conn, $query);   
  if ($resource == false) {
   exit("Database error: $query<br />" . mysqli_error($conn));
  }
  mysqli_close($conn); //close connection
}

/**
 * Deletes internship posting records based on given $id parameter value. 
 * Exits application if any database error occurs. 
 * NOT REQUIRED for the 1st iteration.
 * @param integer $id - ID value to uniquely identify an internship posting.
 */
function deleteInternship($id) 
{
  $query = "delete from internship where internshipID='$id'";
  $conn = connect( ); //open connection
  $resource = mysqli_query($conn, $query);   
  if ($resource == false) {
   exit("Database error: $query<br />" . mysqli_error($conn));
  }
  mysqli_close($conn); //close connection
}

/**
 * Queries the database to get all the internship postings.
 * @return indexed array of associative arrays, where inner associative arrays
 * hold internship posting records with column names as keys.
 */
// function selectAllInternships()
// {
 // $internships = array( );
  // $conn = connect( ); //open connection
  // $query = "select * from internship"; //define $query with query string
  // $resource = mysqli_query($conn, $query); 
  // if ($resource == false) {
   // exit("Database error: $query<br />" . mysqli_error($conn));
  // }
  // mysqli_close($conn); //close connection
  
  // for ($row = mysqli_fetch_assoc($resource); $row != NULL; 
       // $row = mysqli_fetch_assoc($resource)) {
    // $internships[ ] = $row;
  // }
  // return $internships;
// }

/**
 * Sorts the data based on a given $sortCriterion. 
 * @param string $sortCriterion - name of the column based on which sorting 
 * is done.
 * NOT REQUIRED for the 1st iteration.
 * @return indexed array with sorted records or NULL if $sortCriteria does not
 * match any of the table's columns.
 */
function selectSortedInternships($sortCriterion)
{
  $internships = array( );
  $conn = connect( ); //open connection
  $query = "select * from internship order by $sortCriterion"; //define $query with query string
  $resource = mysqli_query($conn, $query); 
  if ($resource == false) {
   exit("Database error: $query<br />" . mysqli_error($conn));
  }
  mysqli_close($conn); //close connection
  
  for ($row = mysqli_fetch_assoc($resource); $row != NULL; 
       $row = mysqli_fetch_assoc($resource)) {
    $internships[ ] = $row;
  }
  return $internships;
}

 ?>